<!doctype html>
<html>
  <head>
    <title>Notifications: ServiceWorkerRegistration.showNotification().</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../serviceworker/resources/test-helpers.js"></script>
    <script src="resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Tests that the showNotification() function resolves a promise, that the
      // notificationclick event gets fired on the Service Worker when we simulate a
      // click on it, and the notification can then be closed. This test requires
      // the test runner.
      async_test(function(test) {
          var scope = 'resources/scope/serviceworkerregistration-document-close';
          var script = 'instrumentation-service-worker.js';
          var port;

          testRunner.setPermission('notifications', 'granted', location.origin, location.origin);

          getActiveServiceWorkerWithMessagePort(test, script, scope).then(function(info) {
              port = info.port;

              // (1) Show a notification from the document. It will be closed by the service worker.
              assert_inherits(info.registration, 'showNotification', 'showNotification() must be exposed.');
              return info.registration.showNotification(scope, {
                  body: 'ACTION:CLOSE',
                  icon: '/icon.png'
              });
          }).then(function() {
              // (2) Simulate a click on the notification that has been displayed.
              return simulateNotificationClick(scope, -1 /* action_index */, port);
          }).then(function(data) {
              // (3) Verify that the correct notification was closed.
              assert_equals(event.data.notification.title, scope);
              return sendCommand(port, { command: 'get' });
          }).then(function(data) {
              // (4) Verify there are no more open notifications.
              assert_equals(data.notifications.length, 0);

              test.done();
          }).catch(unreached_rejection(test));

      }, 'Clicking on a notification displayed through showNotification() fires a Service Worker event, and can be closed there.');
    </script>
  </body>
</html>
